local liyong = fk.CreateSkill {
  name = "lvshi__liyong",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["lvshi__liyong"] = "励勇",
  [":lvshi__liyong"] = "限定技，若你的身份为忠臣/反贼/内奸，当主公/反贼/你进入濒死状态时，你可令其回复2点体力，然后你视为在所有其他角色的攻击范围内。",

  ["#lvshi__liyong-invoke"] = "励勇：你可以令 %dest 回复2点体力，然后你视为在所有其他角色的攻击范围内",

  ["$lvshi__liyong1"] = "旧发不断，新发难生。",
  ["$lvshi__liyong2"] = "切发之誓，苍天可鉴。",
}

-- 获取目标角色基于身份
local function getTargetByRole(player, dyingPlayer)
  local room = player.room
  if player.role == "loyalist" then
    -- 忠臣：主公
    local lord = room:getPlayerBySeat(1)
    return lord and lord == dyingPlayer and lord or nil
  elseif player.role == "rebel" or player.role == "rebel_chief" then
    -- 反贼：任意反贼进入濒死状态都可以触发
    return (dyingPlayer.role == "rebel" or dyingPlayer.role == "rebel_chief") and dyingPlayer or nil
  elseif player.role == "renegade" then
    -- 内奸：只有自己进入濒死状态才能触发
    return player == dyingPlayer and player or nil
  end
  return nil
end

liyong:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:usedSkillTimes(liyong.name, Player.HistoryGame) > 0 or not player:hasSkill(liyong.name) then return end
    if target == player or target.role_shown then
      local targetRole = getTargetByRole(player, target)
      if not targetRole or targetRole ~= target then return end
      return target.dying and target:isWounded()
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = liyong.name,
      prompt = "#lvshi__liyong-invoke::"..target.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 令目标回复2点体力
    room:recover{
      who = target,
      num = 2,
      recoverBy = player,
      skillName = liyong.name,
    }
    if player.dead then return end
    -- 添加攻击范围效果：视为在所有其他角色的攻击范围内
    -- 这里使用一个标记来记录这个效果
    room:setPlayerMark(player, "liyong_attack_range_effect", 1)
  end,
})

-- 攻击范围效果：让玩家被视为在所有其他角色的攻击范围内
-- 这意味着其他角色都可以攻击到玩家
liyong:addEffect("atkrange", {
  within_func = function (self, from, to)
    return to:getMark("liyong_attack_range_effect") > 0
  end,
})

return liyong